import xadmin
from django.contrib import admin

# Register your models here.
from xadmin.views.website import LoginView
from xadmin.views import CommAdminView
from django.utils.translation import ugettext_lazy as _

from .models import User
from xadmin.plugins import auth



class LoginViewAdmin(LoginView):
    title = 'czll博客管理系统'


class GlobalSetting(CommAdminView):
    # 左上角及浏览器标题
    site_title = 'czll博客管理系统'
    # 页脚版权信息
    site_footer = 'Copyright  2018 axyao'
    menu_style = 'accordion'

# 自定义构建模块信息
class UserProfileAdmin(auth.UserAdmin):
    def __init__(self,request, *args, **kwargs):
        self.list_display = ('username', 'email', 'is_active', 'is_superuser',)
        self.search_fields = ('username', 'email')
        self.list_per_page = 10
        # self.form = UserProfileChangeForm  # 编辑用户表单，使用自定义的表单
        # self.add_form = UserProfileCreationForm  # 添加用户表单，使用自定义的表单
                # 以上的属性都可以在django源码的UserAdmin类中找到，我们做以覆盖
    #     # 这个方法在源码的admin/options.py文件的ModelAdmin这个类中定义，我们要重新定义它，以达到不同权限的用户，返回的表单内容不同
        if not request.user.is_superuser:  # 非super用户不能设定编辑是否为super用户
            self.fieldsets = ((None, {'fields': ('username', 'password',)}),
                              (_('Personal info'), {'fields': ('tel','gender','realname', 'email')}),
                              # _ 将('')里的内容国际化,这样可以让admin里的文字自动随着LANGUAGE_CODE切换中英文
                              (_('Permissions'), {'fields': ('is_active', 'is_staff', 'groups')}),
                              (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
                              )  # 这里('Permissions')中没有'is_superuser',此字段定义UserChangeForm表单中的具体显示内容，并可以分类显示
            self.add_fieldsets = ((None, {'classes': ('wide',),
                                          'fields': ('username', 'name', 'password1', 'password2', 'email', 'is_active',
                                                     'is_staff', 'groups'),
                                          }),
                                  )  #此字段定义UserCreationForm表单中的具体显示内容
        else:  # super账户可以做任何事
            self.fieldsets = ((None, {'fields': ('username', 'password',)}),
                              (_('Personal info'), {'fields': ('nickname','realname', 'email','tel','gender')}),
                              (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups')}),
                              (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
                              )
            self.add_fieldsets = ((None, {'classes': ('wide',),
                                          'fields': ('username', 'name', 'password1', 'password2', 'email', 'is_active',
                                                     'is_staff', 'is_superuser', 'groups'),
                                          }),
                                  )
        return  super(UserProfileAdmin,self).__init__(request, *args, **kwargs)
    # def get_form_layout(self,*args, **kwargs):
    #     self.fieldsets = ((None, {'fields': ('username', 'password',)}),
    #                       (_('Personal info'), {'fields': ('nickname', 'realname', 'email', 'tel', 'gender')}),
    #                       (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups')}),
    #                       (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    #                       )
    #     self.add_fieldsets = ((None, {'classes': ('wide',),
    #                                   'fields': ('username', 'name', 'password1', 'password2', 'email', 'is_active',
    #                                              'is_staff', 'is_superuser', 'groups'),
    #                                   }),
    #                           )
    #     return super(UserProfileAdmin, self).get_form_layout()

    #         return super(UserProfileAdmin,self).changelist_view(request,extra_context)


#模块注册
xadmin.site.unregister(User)
xadmin.site.register(User, UserProfileAdmin)
xadmin.site.register(CommAdminView, GlobalSetting)
xadmin.site.register(LoginView, LoginViewAdmin)